import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/extension/future_error_handler.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../../../../l10n/local.dart';
import '../../../../../routes/app_routes.dart';
import '../../../../login_register/area_code/country_model.dart';
import '../../../../login_register/forms/email_register_form.dart';
import '../../../../login_register/forms/forget_pwd_form.dart';
import '../../../../login_register/forms/phone_login_form.dart';
import '../../../../mixin/account_security_mixin.dart';
import '../../../../models/user_model.dart';
import 'controller.dart';

class VerifyCompleteView extends BaseGetView<VerifyCompleteController>
    with AccountSecurityMixin {
  VerifyCompleteView({super.key});

  @override
  String get title {
    switch (controller.state.param.securityType) {
      case SecurityType.resetPwd:
        return local.input_new_password;
      case SecurityType.updateEmail:
        return local.input_new_email;
      case SecurityType.updatePhone:
        return local.input_new_phone;
      default:
        return '';
    }
  }

  @override
  Widget buildBody(BuildContext context) {
    return DismissKeyboardScaffold(
      resizeToAvoidBottomInset: false, // 防止键盘弹出时页面内容被顶起
      child: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.black,
        padding: EdgeInsets.only(
          left: CONTENT_MARGIN,
          right: CONTENT_MARGIN,
          top: CONTENT_MARGIN * 2,
        ),
        child: SingleChildScrollView(
          // 添加滚动
          physics: const ClampingScrollPhysics(), // 防止滚动回弹
          child: Column(
            children: [
              if (controller.state.param.securityType ==
                  SecurityType.resetPwd) ...[
                ForgetPasswordForm(
                  key: const ValueKey('forgot'),
                  onSwitchAuthMode: (_) {},
                  sendVerificationCodeAction: (email, p1) {},
                  resetPasswordWithEmailAction: (p0, p1, p2) {
                    // p0: email, p1: password, p2: verificationCode
                    // 修改密码
                    controller.resetPasswordOnTap(p1);
                  },
                  isRegisterMode: false,
                ),
              ],
              if (controller.state.param.securityType ==
                  SecurityType.updateEmail) ...[
                EmailRegisterForm(
                  key: const ValueKey('email_register'),
                  onSwitchAuthMode: (_) {},
                  sendVerificationCodeAction: (email, p1) {
                    // 发送验证码，把邮箱传出去，同时外面传进来一个bool值表示是否发送成功
                    accountSecurityRequestVerificationCode(
                      account: email,
                      platform: CodePlatform.email,
                      codeType: CodeType.bind,
                    ).then((value) => p1(value)).catchErrorHandler();
                  },
                  registerWithEmailAction: (p0, p1, p2) {
                    // p0: email, p1: password, p2: verificationCode
                    // 绑定新邮箱
                    controller.bindNewPhoneOrEmailOnTap(p0, p2, isEmail: true);
                  },
                  isRegisterMode: false,
                )
              ],
              if (controller.state.param.securityType ==
                  SecurityType.updatePhone) ...[
                Obx(
                  () => PhoneLoginForm(
                    key: const ValueKey('phone_login'),
                    onSwitchAuthMode: (_) {},
                    sendVerificationCodeAction: (phone, p1) {
                      // 发送验证码
                      accountSecurityRequestVerificationCode(
                        account: "${controller.state.formatAreaCode}$phone",
                        platform: CodePlatform.phone,
                        codeType: CodeType.bind,
                      ).then((value) => p1(value)).catchErrorHandler();
                    },
                    loginWithPhoneAction: (phone, code) {
                      // 绑定新手机号
                      controller.bindNewPhoneOrEmailOnTap(
                          "${controller.state.formatAreaCode}$phone", code);
                    },
                    thirdLoginAction: () {},
                    areaCode: controller.state.phoneCode.value,
                    areaCodeOnTap: () {
                      // 选择国家码
                      Get.toNamed(AppRoutes.AREA_CODE,
                              arguments: controller.state.phoneCode.value)
                          ?.then((value) {
                        if (value != null && value is AreaCodeModel) {
                          controller.state.phoneCode.value = value.phoneCode;
                        }
                      });
                    },
                    isRegisterMode: false,
                  ),
                )
              ]
            ],
          ),
        ),
      ),
    );
  }
}
